package com.hnx.admin.common.security.aop;

import cn.hutool.core.util.ObjectUtil;
import com.hnx.admin.common.security.core.SecurityProperties;
import com.hnx.admin.common.security.utils.RequestContextHolderUtil;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;

/*
创建人： 倔强的头发
创建时间：2024/7/10
功能描述:

*/

@Aspect
@Component
@RequiredArgsConstructor
public class ErrorVerifyAspect {
    private final SecurityProperties securityProperties;

    private final com.hnx.admin.common.security.core.VerifyError verifyError;
    @Pointcut("execution(* com.hnx.admin.common.security.handler.*.onAuthenticationFailure(..))")
    public void pointcut() {}
    @Before("pointcut()")
    public void verify(){
        Map<String, Object> login = RequestContextHolderUtil.getContext();
        if(ObjectUtil.isNotEmpty(login)) {
            Object username = login.get(securityProperties.getUsername());
            if (ObjectUtil.isNotEmpty(username)) {
                String nameKey = verifyError.getRetryKey(username.toString().trim());
                verifyError.setRetryCount(nameKey, verifyError.getRetryCount(nameKey));
            }
        }
    }
}
